package com.fadadianqi.logistics.admin;

import com.fadadianqi.commons.ConfigHelper;
import com.yeqiangwei.commons.util.DateTimeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class DataBackup {

    private static final Log LOG = LogFactory.getLog(DataBackup.class);

    static final String OSNAME = System.getProperty("os.name");

    public void backup() {
        String backupPath = getBackDir() + getPrefixName() + getSuffixName();
        String cmd = null;
        if ("linux".equalsIgnoreCase(OSNAME) || "unix".equalsIgnoreCase(OSNAME)
                || OSNAME.equalsIgnoreCase("freebsd")) {
            cmd = "mysqldump -h" + getDBHost() + " -uroot -p" + getDBpassword()
                    + " " + getDBname() + " > " + backupPath;
        } else {
            String dir = System.getProperty("base.dir", "..");
            cmd = dir + "mysqldump -h" + getDBHost() + " -uroot -p" + getDBpassword()
                    + " " + getDBname() + " --opt > " + backupPath;
        }
        LOG.info(cmd);
        Process process;
        InputStream inputStream = null;
        try {
            process = Runtime.getRuntime().exec(cmd);
            StringBuffer sb = new StringBuffer();
            inputStream = process.getInputStream();
            InputStreamReader ir = new InputStreamReader(inputStream, "utf-8");
            LineNumberReader input = new LineNumberReader(ir);
            String line = null;
            while ((line = input.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
                LOG.info(line);
            }
            FileOutputStream fout = new FileOutputStream(backupPath);
            OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
            writer.write(sb.toString());
            writer.flush();
            writer.close();
            input.close();
            ir.close();
            fout.close();
        } catch (Exception e1) {
            LOG.error(e1.toString());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOG.error(e.toString());
                }
            }
        }
    }

    private String getDBpassword() {
        return ConfigHelper.getString("admin.db.password", "qwe123");
    }

    private String getDBname() {
        return ConfigHelper.getString("admin.db.name", "fadadianqi");
    }

    private String getBackDir() {
        return ConfigHelper.getString("admin.db.backup.dir", File.separator);
    }

    private String getDBHost() {
        return ConfigHelper.getString("admin.db.host", "127.0.0.1");
    }

    /**
     * 备份文件的前缀
     *
     * @return
     */
    private String getPrefixName() {
        return ConfigHelper.getString("admin.db.backup.prefixName");
    }

    private String getSuffixName() {
        long timeInMillis = DateTimeUtils.getTimeInMillis();
        return DateTimeUtils.format(timeInMillis, "yyyyMMddHHmmss");
    }

}
